################################################################################
# Title   : Replication Code — Union Spillovers Paper
# Project : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
# Authors : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
# Output  : Figures 2
# Date    : August 2025
################################################################################

# Set default graphical theme
theme_set(theme_light())

# Create output directory if it doesn't exist
if (!dir.exists(figs)) dir.create(figs, recursive = TRUE)

# Create the data frame
data <- read_dta(paste0(wd,"/transition_to_union.dta")) %>% 
  mutate(time = as.numeric(time))

# Reshape the data to long format
data_long <- data %>%
  pivot_longer(cols = c(n_to_u, u_to_u), names_to = "Series", values_to = "Value")

# Rename the series labels
data_long$Series <- dplyr::recode(data_long$Series,
                                  "n_to_u" = "Non-Union-to-Union",
                                  "u_to_u" = "Union-to-Union")

# Generate the plot
ggplot(data_long, aes(x = time, y = Value, color = Series, shape = Series)) +
  geom_line() +
  geom_point(size = 3) +
  geom_text_repel(
    aes(x = time, y = Value, label = round(Value, 3)),
    size = 4,
    nudge_y = 0.01,
    segment.color = NA,
    show.legend = FALSE
  ) +
  labs(x = "Year",
       y = "Transition Probabilities",
       color = NULL,
       shape = NULL) +       # Removes the legend titles
  #  theme_minimal() +
  theme(
    legend.position = c(0.95, 0.95),
    legend.justification = c("right", "top"),
    legend.background = element_rect(fill = "white", color = "black")
  ) 


ggsave(paste0(figs,"/Figure_2.pdf"),
       units = c("in"),
       height = 5,
       width = 8)